/**
 * Copyright &copy; 2016-2017 <a href="http://git.oschina.net/whoamien/backend_management">Backend Management</a> All rights reserved.
 */
package com.xw.app.imageupload;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;

import com.qcloud.cosapi.api.CosCloud;
import com.xw.util.JsonUtil;


/**
 * The Class QCloudImageUploadMethod.
 *
 * @author Xi Wei
 */
public class QCloudImageUploadMethod implements ImageUploadMethod {

	/** The bucket name. */
	public String bucketName = "leyoobao";

	/** The app id. */
	public String appId = "10011323";

	/** The secret id. */
	public String secretId = "AKIDce1H2qqM7b94jcb8FPlQXoxOycjHQ9Dt";

	/** The secret key. */
	public String secretKey = "AnBSEYLyXq50hZlPXS0qLdK08sJx6EHF";

	private void initProperties() {
		Properties prop = new Properties();
		try {
			prop.load(getClass().getResourceAsStream("/qcloudoss.properties"));
			bucketName = prop.getProperty("qcloud.bucketName");
			appId = prop.getProperty("qcloud.appId");
			secretId = prop.getProperty("qcloud.secretId");
			secretKey = prop.getProperty("qcloud.secretKey");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/* (non-Javadoc)
	 * @see com.xw.app.imageupload.ImageUploadMethod#uploadImage(javax.servlet.http.HttpServletRequest, org.springframework.web.multipart.MultipartFile, java.lang.String, java.lang.String)
	 */
	@Override
	public String uploadImage(HttpServletRequest request, MultipartFile file, String relativePath, String fileName) {
		initProperties();
		CosCloud cos = new CosCloud(Integer.valueOf(appId), secretId, secretKey);
		try {
			createFolder(relativePath);
			String returnString = cos.uploadFile(bucketName, relativePath + "/" + fileName, file.getInputStream());
			System.out.println("======returnString =====" + returnString);
			QCloudUploadReturn returnBean = (QCloudUploadReturn) JsonUtil.jsonToBean(returnString,
					QCloudUploadReturn.class);

			return returnBean.getData().getAccessUrl();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * Creates the folder.
	 *
	 * @param path the path
	 * @return true, if successful
	 */
	public final boolean createFolder(String path) {
		if (!path.startsWith("/")) {
			path = "/" + path;
		}
		if (!path.endsWith("/")) {
			path = path + "/";
		}
		CosCloud cos = new CosCloud(Integer.valueOf(appId), secretId, secretKey);

		boolean success = false;
		String[] pathArray = path.split("/");
		String createPath = "/";
		for (int i = 1; i < pathArray.length; i++) {
			createPath = createPath + pathArray[i] + "/";
			try {
				cos.createFolder(bucketName, createPath);
				success = true;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				success = false;
				break;
			}

		}
		return success;

	}

	/* (non-Javadoc)
	 * @see com.xw.app.imageupload.ImageUploadMethod#uploadImage(javax.servlet.http.HttpServletRequest, java.io.InputStream, java.lang.String, java.lang.String)
	 */
	@Override
	public String uploadImage(HttpServletRequest request, InputStream is, String relativePath, String fileName) {
		initProperties();
		CosCloud cos = new CosCloud(Integer.valueOf(appId), secretId, secretKey);
		try {
			createFolder(relativePath);
			String returnString = cos.uploadFile(bucketName, relativePath + "/" + fileName, is);
			System.out.println("======returnString =====" + returnString);
			QCloudUploadReturn returnBean = (QCloudUploadReturn) JsonUtil.jsonToBean(returnString,
					QCloudUploadReturn.class);

			return returnBean.getData().getAccessUrl();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

}
